//
//  AppDelegate.swift
//  我的微博
//
//  Created by ZhangChao on 15/8/31.
//  Copyright © 2015年 ZhangChao. All rights reserved.
//

import UIKit
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        //打印已经存储的用户信息
        printLog(UserAccountViewModel.sharedUserAccount.userAccount)
        
        setupAppearance()
        setUpNetIndecater()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.rootViewController = defaultRootViewController()
        
        window?.makeKeyAndVisible()
        
        // 发送通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootViewController:", name: SwitchRootViewControllerNotification, object: nil)
        
        return true
        
    }
    
    // 用通知跳转界面
    func switchRootViewController (notification: NSNotification){
        
        printLog(notification)
        window?.rootViewController = (notification.object != nil) ? WelcomeViewController() : MainViewController()
        
    }
    // 注销通知
    deinit{
        
        NSNotificationCenter.defaultCenter().removeObserver(SwitchRootViewControllerNotification)
    }
    // 判断用户的登录状态,然后决定跳转到哪个控制器
    
    private func defaultRootViewController() -> UIViewController{
        
        // 1.判断用户是否登录
        if UserAccountViewModel.sharedUserAccount.userLogon {
            
            return isNewVersion() ? NewFeatureViewController() : WelcomeViewController()
        }
        
        // 如果没有登录 返回main
        return MainViewController()
    }
    

    // 设置全局外观
    private func setupAppearance(){
        
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
    // 设置网络指示器
    private func setUpNetIndecater(){
        
        // 只有AFN框架中的网络请求有用,别的框架没有
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        
        // 设置缓存的大小
        // memoryCapacity 内存缓存大小
        // diskCapacity 磁盘缓存大小
        // NSURLSesstion 中只有dataTask会存在缓存,download和upload不会产生缓存
        let cache = NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil)
        
        NSURLCache.setSharedURLCache(cache)
        
    }

    // 判断是不是新版本
    private func isNewVersion() -> Bool{
        // 1.获取当前的版本号
//        printLog(NSBundle.mainBundle().infoDictionary)
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        
        printLog("当前版本号 \(currentVersion)")
        
        // 2.获取之前的版本号
        let versionKey = "weibo09.com"
        let preVersion = NSUserDefaults.standardUserDefaults().doubleForKey(versionKey)
        printLog("以前的版本号 \(preVersion)")
        
        // 3.保存当前的版本
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: versionKey)
        
        // 4.比较俩个值,返回比较结果
        return currentVersion > preVersion
    
    }
}

